package cn.itcast.health.admin.controller;

import cn.itcast.health.admin.logs.OperationLog;
import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.commons.utils.LogUtils;
import cn.itcast.health.commons.utils.POIUtils;
import cn.itcast.health.interfaces.service.OrdersettingService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;


    @PostMapping("/importOrderSettings.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.MODIFY, operationContent = "根据上传excel文件修改预约设置的数据，注意此修改包含添加根据日期唯一性完成的")
    public Result importOrderSettings(@RequestParam("excelFile") MultipartFile file){
        List<String[]> filelist = null;
        try {
            POIUtils.checkFile(file);
            filelist = POIUtils.readExcel(file);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        Result result = ordersettingService.importOrderSettings(filelist);
        return result;
    }


    /**
     *根据年和月 查询可以预约的数据
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/findOrderSettingByDate.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.QUERY, operationContent = "查询一个月内的预约设置数据")
    public Result findOrderSettingByDate(@RequestParam("year")String year , @RequestParam("month") String month){
        Result result = ordersettingService.findOrderSettingByDate(year,month);
        return result;
    }

    @PutMapping("/updateNumbersByDate.do")
    @OperationLog(moduleName = LogUtils.Module.SYSTEM, operationType = LogUtils.Type.MODIFY, operationContent = "根据天数修改当前天的可预约的数量")
    public Result updateNumbersByDate(@RequestBody Map map){
        Result result = ordersettingService.updateNumbersByDate(map);
        return result;
    }
}
